#include "CGlobal.h"
#include "CCEGLView.h"
#include "SimpleAudioEngine.h"
#include "ConstDefine.h"
//#include "exitDlg.h"
//#include "ExitLayer.h"
//#include "FileMethod.h"
//#include "platform.h"
using namespace cocos2d;

cocos2d::CCSize CGlobal::GetResolution( )
{
	return cocos2d::CCEGLView::sharedOpenGLView()->getFrameSize();
	//	return cocos2d::CCEGLView::sharedOpenGLView().getSize();
}



float CGlobal::GetRatioX()
{
	return CGlobal::GetResolution().width/640;
}
float CGlobal::GetRatioY()
{
	return CGlobal::GetResolution().height/960;
}

cocos2d::CCSprite * CGlobal::SpriteWithFile(const char *pName )
{
	CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage(pName);
	if (pTexture)
	{
		CCSprite *pSprite=CCSprite::spriteWithTexture(pTexture);
		return pSprite;
	}
	return NULL;
}

cocos2d::CCTexture2D * CGlobal::TextureWithFile(const char *pName )
{
	return CCTextureCache::sharedTextureCache()->addImage(pName);
}

cocos2d::CCLabelTTF * CGlobal::GetLocalTime()
{
	/*	cocos2d::cc_timeval now;
	cocos2d::CCTime::gettimeofdayCocos2d(&now, NULL); */
	struct tm  *timeStuct=NULL;
	time_t time_Sys=time(NULL)/*now.tv_sec*/;
	timeStuct = localtime(&time_Sys);  
	if (timeStuct==NULL)
	{
		return NULL;
	}
	char buf[50]={0};
	std::string str1="";
	std::string str2="";
	if (timeStuct->tm_hour<10)
	{
		str1="0";
	}

	if (timeStuct->tm_min<10)
	{
		str2="0";
	}

	sprintf(buf,"%s%d:%s%d",str1.c_str(),timeStuct->tm_hour,str2.c_str(),timeStuct->tm_min);
	CCLabelTTF *pLable=CCLabelTTF::labelWithString(buf,"Arial",24*CGlobal::GetRatioX());
	if (pLable)
	{
		pLable->setOpacity(100);
		ccColor3B col;
		col.r=65;
		col.g=189;
		col.b=250;
		pLable->setColor(col);
	}
	return pLable;
}

void CGlobal::UpdateLocalTime( cocos2d::CCLabelTTF* pLable )
{
	if (pLable==NULL)
	{
		return ;
	}
	struct tm  *timeStuct=NULL;
	time_t time_Sys=time(NULL)/*now.tv_sec*/;
	timeStuct = localtime(&time_Sys);  
	if (timeStuct==NULL)
	{
		return;
	}
	std::string str1="";
	std::string str2="";
	if (timeStuct->tm_hour<10)
	{
		str1="0";
	}

	if (timeStuct->tm_min<10)
	{
		str2="0";
	}

	char buf[50]={0};
	sprintf(buf,"%s%d:%s%d",str1.c_str(),timeStuct->tm_hour,str2.c_str(),timeStuct->tm_min);
	pLable->setString(buf);
}

void CGlobal::Exit()
{
	CGlobal::StopAllMusic();
	CCDirector::sharedDirector()->end();
}
void CGlobal::StopAllMusic()
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->stopAllEffects();
	CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
}

cocos2d::CCSize CGlobal::GetPlayerSize()
{
	return CCSize(121*CGlobal::GetRatioX(),126*CGlobal::GetRatioY());
}

void CGlobal::initGrid()
{
	int objWidth=100*CGlobal::GetRatioX();
	int objHeight=100*CGlobal::GetRatioY();
	int nXNum=(CGlobal::GetResolution().width-objWidth)/objWidth;
	int nYNum=(g_edgeDistance*PTM_RATIO -objHeight)/objHeight;
	for (int y=1;y<nYNum+1;y++)
	{
		for (int x=1;x<nXNum+1;x++)
		{
			netGridding_.push_back(std::make_pair(x,y) );
		}
	}
}	

void CGlobal::getGrid( std::vector<std::pair<int,int> > &netGridding )
{
	//netGridding.insert(netGridding_.begin())
	if (netGridding_.size()==0)
	{
		initGrid();
	}
	netGridding=netGridding_;
}

bool CGlobal::isXCrossLimit( const cocos2d::CCPoint &ptPos,const cocos2d::CCSize &objSize )
{
	if (ptPos.x+objSize.width/2>=CGlobal::GetResolution().width )
	{
		return true;
	}
	if (ptPos.x-objSize.width/2<=0)
	{
		return true;
	}
	return false;
	 
}

CCFiniteTimeAction * CGlobal::getDelayFun( float fSec,CCObject *pParent,const cocos2d::SEL_CallFuncN &callFun )
{
	CCDelayTime *pDelayTime=CCDelayTime::actionWithDuration(fSec);
	CCCallFuncN *pCallFunc= CCCallFuncN::actionWithTarget(pParent,callFun);
	if (pDelayTime&&pCallFunc)
	{
		CCFiniteTimeAction *pSeq=CCSequence::actions(pDelayTime,pCallFunc,NULL);
		return pSeq;
	}
	return NULL;
}



std::vector<std::pair<int,int> > CGlobal::netGridding_;


